<template>
  <div>
    <el-table :data="skuList" border>
      <el-table-column type="index" label="序号" width="80" align="center" />
      <el-table-column prop="skuName" label="名称" width="width" />
      <el-table-column prop="skuDesc" label="描述" width="width" />
      <el-table-column label="默认图片" width="120">
        <template slot-scope="{row}">
          <el-image :src="row.skuDefaultImg" style="width: 80px; height: 80px" />
        </template>
      </el-table-column>
      <el-table-column prop="weight" label="重量(KG)" width="120" />
      <el-table-column prop="price" label="价格" width="120" />
      <el-table-column prop="prop" label="操作" width="width">
        <template slot-scope="{row}">
          <el-button v-if="row.isSale === 0" type="success" icon="el-icon-sort-down" size="mini" @click="sale(row)" />
          <el-button v-else style="background-color: #909399; border-color: #909399;" type="success" icon="el-icon-sort-up" size="mini" @click="cancel(row)" />
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit(row)" />
          <el-button type="info" icon="el-icon-info" size="mini" @click="getSkuInfo(row)" />
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDelete(row)" />
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="text-align: center"
      :current-page="page"
      :page-sizes="[5, 10, 15]"
      :page-size="limit"
      layout="prev, pager, next, jumper, -> ,total, sizes"
      :total="total"
      @current-change="getSkuList"
      @size-change="handleSizeChange"
    />
    <!-- 抽屉界面 -->
    <el-drawer :visible.sync="show" :show-close="false" size="50%">
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{ skuInfo.price }}元</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="16">
          <template slot-scope="">
            <el-tag v-for="(attr) in skuInfo.skuAttrValueList" :key="attr.id" type="success" style="margin-right: 10px;">{{ attr.attrId }}--{{ attr.valueId }}</el-tag>
          </template>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">
          <template>
            <el-carousel :interval="2000" :autoplay="true">
              <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id" style="height: 500px">
                <img :src="item.imgUrl" style="height: 100%;">
              </el-carousel-item>
            </el-carousel>
          </template>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: 'Sku',
  data() {
    return {
      page: 1,
      limit: 5,
      skuList: [],
      total: 0,
      skuInfo: {},
      show: false
    }
  },
  mounted() {
    // 获取SKU列表
    this.getSkuList()
  },
  methods: {
    async getSkuList(pages = 1) {
      this.page = pages
      const { page, limit } = this
      const result = await this.$API.sku.reqSkuList(page, limit)
      if (result.code === 200) {
        this.skuList = result.data.records
        this.total = result.data.total
      }
    },
    // 更新页码
    handleSizeChange(limit) {
      this.limit = limit
      this.getSkuList()
    },
    // 商品上架
    async sale(row) {
      const result = await this.$API.sku.reqSale(row.id)
      if (result.code === 200) {
        this.$message.success('上架成功')
        this.getSkuList(this.page)
      }
    },
    // 商品下架
    async cancel(row) {
      const result = await this.$API.sku.reqCancel(row.id)
      if (result.code === 200) {
        // row.isSale = 0
        this.$message.success('下架成功')
        this.getSkuList(this.page)
      }
    },
    // 商品修改
    edit() {
      this.$message('正在开发中')
    },
    // 获取SKU详情
    async getSkuInfo(row) {
      const result = await this.$API.sku.reqSkuById(row.id)
      if (result.code === 200) {
        this.skuInfo = result.data
        this.show = true
      }
    },
    // 删除SKU
    async handleDelete(row) {
      const result = await this.$API.sku.reqDelete(row.id)
      if (result.code === 200) {
        this.$message.success('删除成功')
        this.getSkuList(this.page)
      }
    }
  }
}
</script>

<style scoped>
  .el-row .el-col-5 {
    font-size: 18px;
    text-align: right
  }
  .el-col {
    margin: 10px 10px;
  }
  .el-carousel__item h3 {
    color: #475669;
    font-size: 18px;
    opacity: 0.75;
    line-height: 300px;
    margin: 0;
  }
  .el-carousel__item:nth-child(2n) {
    background-color: #99a9bf;
  }
  .el-carousel__item:nth-child(2n+1) {
    background-color: #d3dce6;
  }
  .el-carousel--horizontal {
    height: 500px;
  }
  >>>.el-carousel__container {
    height: 500px;
  }
  >>>.el-carousel__indicators--horizontal{
    bottom: 50px;
  }
  >>>.el-carousel__button{
    background-color: #6b0000;
  }
</style>
